package com.tr.rentcar.controller.userc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.bean.Order;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserRecord;
import com.tr.rentcar.model.dao.OrderDao;
import com.tr.rentcar.model.dao.UserDao;
import com.tr.rentcar.model.dao.UserRecordDao;

/**
 * Servlet implementation class UserOrder
 */
@WebServlet("/user/order")
public class UserOrdersController extends HttpServlet {
	OrderDao ua=new OrderDao();
	UserDao ud=new UserDao();
	UserRecordDao ur=new UserRecordDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String orderState=request.getParameter("orderstate");
		User user=(User) request.getSession().getAttribute("userinfo");
		String ostatus=request.getParameter("ostatus");
		List<Order> usero = null;//储存全部订单
		try {
			usero = ua.getById(user.getUid(),0);
			User u=ud.findByid(user.getUid());
			request.setAttribute("ostatus", "全部");
			request.setAttribute("AllCount", ua.getAllCountById(user.getUid()));
			request.getSession().setAttribute("umoney", u.getUmoney());
		
		if (orderState.equals("1")) {
				if (StringUtils.isEmpty(ostatus)||ostatus.equalsIgnoreCase("全部")) {
					request.setAttribute("ostatus", "全部");
					request.setAttribute("usero", usero);
					request.setAttribute("titlew", "全部订单");
				}else {
						List <Order> userorder=ua.getByIdState(user.getUid(), ostatus,0);
						request.setAttribute("ostatus", ostatus);
						request.setAttribute("AllCount", ua.getAllCountByIdState(user.getUid(),ostatus));
						request.setAttribute("usero", userorder);
						request.setAttribute("titlew", "全部订单");
				}
				
		}else if(orderState.equals("2")){
			request.setAttribute("AllCount", ua.getAllCountByIdState(user.getUid(),"租赁中"));	
			request.setAttribute("ostatus", "租赁中");
			usero=ua.getByIdState(user.getUid(), "租赁中", 0);
			request.setAttribute("usero", usero);
			request.setAttribute("titlew", "租赁中订单");
			
		}else if(orderState.equals("3")){
			request.setAttribute("AllCount", ua.getAllCountByIdState(user.getUid(),"已还车"));
			request.setAttribute("ostatus","已还车");
			usero=ua.getByIdState(user.getUid(), "已还车", 0);
			request.setAttribute("usero", usero);
			request.setAttribute("titlew", "已还车订单");
		}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		request.setAttribute("p", orderState);
		request.getRequestDispatcher("/WEB-INF/jsp/user_order.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user=(User) request.getSession().getAttribute("userinfo");
		Double omoney=Double.valueOf(request.getParameter("omoney"));
		int oid=Integer.parseInt(request.getParameter("oid")) ;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String returnTime=request.getParameter("returnTime");
		int tian=Integer.parseInt(request.getParameter("tian")) ;
		PrintWriter out=response.getWriter();
		try {
			Date date = sdf.parse(returnTime);
			Calendar c = Calendar.getInstance();
			c.setTime(date); 
			c.add(Calendar.DAY_OF_MONTH,tian);
			//System.out.println(omoney+"|"+oid+"|"+sdf.format(date)+"|"+sdf.format(c.getTime()) +"|"+tian);
			if (ua.yanqi(omoney, sdf.format(c.getTime()), oid)) {
				ud.updateUmoney(omoney,user.getUid());
				UserRecord urr=new UserRecord();
				urr.setuId(user.getUid());
				urr.setUrWay("扣费");
				urr.setUrMoney(omoney);
				urr.setUrName(user.getUname());
				urr.setUrdoWay("用户");
				ur.save(urr);
				out.println("延期成功");
			};
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
		
		
	}

}


